package uni.call.app;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.net.sip.SipException;
import android.net.sip.SipProfile;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class IncomingCallActivity extends Activity 
{
	public static String StartTimeDate;
	
	public long MyTime;
	public StopWatch MyStopWatch;
	public Timer MyTimer;
	
	public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.incall);
        
        final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        StartTimeDate = sdf.format(cal.getTime());
        
        ImageButton EndBT = (ImageButton)findViewById(R.id.End);
        ToggleButton HoldBT = (ToggleButton)findViewById(R.id.Hold);
        ToggleButton SpeakerBT = (ToggleButton)findViewById(R.id.Speaker);
    	ToggleButton MuteBT = (ToggleButton)findViewById(R.id.Mute);
        
        EndBT.setOnClickListener(EndListener);
        HoldBT.setOnClickListener(HoldListener);
        SpeakerBT.setOnClickListener(SpeakerListener);
        MuteBT.setOnClickListener(MuteListener);
        
        SipProfile TempPeer =  IncomingCallReceiver.IncomingCall.getPeerProfile();
        
        TextView UserName = (TextView)findViewById(R.id.CallerName);
        TextView UserNumber = (TextView)findViewById(R.id.CallerNumber);
        UserName.setText(TempPeer.getDisplayName());
        UserNumber.setText(TempPeer.getUserName());
        try
    	{
    		MyStopWatch = new StopWatch();
    		MyStopWatch.Start();
			IncomingCallReceiver.IncomingCall.answerCall(30);
    		MyTimer = new Timer();
    		MyTimer.schedule(new TimerTask()
    		{
    			@Override
    			public void run()
    			{
    				TimerMethod();
    			}

    		}, 0, 1000);
    		
		}
    	catch (SipException e)
    	{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	IncomingCallReceiver.IncomingCall.startAudio();
    	IncomingCallReceiver.IncomingCall.setSpeakerMode(false);
        
    	if(IncomingCallReceiver.IncomingCall.isMuted())
        {
        	IncomingCallReceiver.IncomingCall.toggleMute();
        }          

	}
	
	private void TimerMethod()
	{
		this.runOnUiThread(Timer_Tick);
	}

	private Runnable Timer_Tick = new Runnable() {
		public void run() {

			UpdateTimer(MyStopWatch.GetElapsedTime());
		}
	};

	public void UpdateTimer(final Long Status)
    {
        this.runOnUiThread(new Runnable()
        {
            public void run()
            {
                TextView TimerTV = (TextView) findViewById(R.id.CallTimer);
                TimerTV.setText(Status.toString());
            }             
        });
    }
    
    private OnClickListener EndListener = new OnClickListener() 
    {
        public void onClick(View v)
        {
        	try 
        	{
        		try
        		{
        			MyTimer.cancel();
        			MyStopWatch.Stop();
        		}
        		catch (Exception e)
        		{
        			MyStopWatch = new StopWatch();
        			MyStopWatch.StartTime= 0;
        			MyStopWatch.StopTime= 0;
				}
        		
        		MyTime =  MyStopWatch.GetElapsedTime();
    			UpdateTimer(MyTime);
    			SipProfile TempPeer =  IncomingCallReceiver.IncomingCall.getPeerProfile();
    			
    			String Duration;
        		if(MyTime/60 < 1 )
        		{
        			Duration = Long.toString(MyTime) + " Sec";
        		}
        		else
        		{
        			Duration = Long.toString(MyTime) + " Min";
        		}
    			
    			Insert("Incoming", TempPeer.getDisplayName(),TempPeer.getUserName(), Duration, StartTimeDate);
        		IncomingCallReceiver.IncomingCall.endCall();

	        	Intent myIntentt = new Intent(getApplicationContext(), HomeActivity.class);
	        	startActivity(myIntentt);
			} 
        	catch (SipException e)
			{
				e.printStackTrace();
			}
        }
    };
    
    private OnClickListener HoldListener = new OnClickListener() 
    {
        public void onClick(View v)
        {
        	ToggleButton HoldBT = (ToggleButton)findViewById(R.id.Hold);
        	if(HoldBT.isChecked())
        	{
	        	try
	        	{
	        		IncomingCallReceiver.IncomingCall.holdCall(0);	
				} 
	        	
	        	catch (SipException e)
				{
				}
        	}	
        	else
        	{
        		try 
        		{
        			IncomingCallReceiver.IncomingCall.continueCall(0);
				} 
        		catch (SipException e) 
				{
					e.printStackTrace();
				}
        	}
        }
    };
    
    private OnClickListener SpeakerListener = new OnClickListener()
    {
        public void onClick(View v)
        {
        	ToggleButton SpeakerBT = (ToggleButton)findViewById(R.id.Speaker);
        	if(SpeakerBT.isChecked())
        	{
        		IncomingCallReceiver.IncomingCall.setSpeakerMode(true);
        	}
        	else
        	{
        		IncomingCallReceiver.IncomingCall.setSpeakerMode(false);
        	}
        }
    };
    
    private OnClickListener MuteListener = new OnClickListener()
    {
        public void onClick(View v)
        {
        	ToggleButton MuteBT = (ToggleButton)findViewById(R.id.Mute);
        	if(MuteBT.isChecked())
        	{
        		IncomingCallReceiver.IncomingCall.toggleMute();
        	}
        	else
        	{
        		IncomingCallReceiver.IncomingCall.toggleMute();
        	}
        }
    };
    
    public void Insert(String Type,String Name, String Number, String Duration, String Time)
    {
		LogDBAdapter myAdapter = new LogDBAdapter(getApplicationContext());
		myAdapter.OpenToWrite();        
        myAdapter.Insert(Type,Name,Number, Duration, Time);
        myAdapter.Close();
    }
}
